// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

var UnitTools = require('UnitTools')
var HotUpdate = require('HotUpdate')
var TableManager = require('TableManager')
var LoginManager = require('LoginManager')
var AudioManager = require('AudioManager')
var ClickManager = require('ClickManager')
var CreatorHelper = require('CreatorHelper')
var Config = require('Config')

cc.Class({
    extends: cc.Component,

    properties: {
        agreeBtn: cc.Toggle,    // 协议按钮
        loginBtn: cc.Node,
        registerBtn: cc.Node,

        loginModel: cc.Node,    // 登录框
        registerModel: cc.Node,    // 注册框
    },

    onLoad () {
        // 版本设置
        var version = cc.find('version', this.node)
        version.getComponent(cc.Label).string = '当前版本: ' + Config.version + ' '

        // 音乐
        CreatorHelper.loadPrefab('prefabs/Bgm', (node) => {
            this.pnode = cc.find('Canvas')
            this.pnode.addChild(node)
            node.getComponent('Bgm')._play()
            this.hotUpdateFn()
        })

        // 登录
        this.loginBtn.on(cc.Node.EventType.TOUCH_END, function (args) {
            if (this.isAgree()) {
                this.loginModel.active = true
            }
        }, this)

        // // 注册
        this.registerBtn.on(cc.Node.EventType.TOUCH_END, function (args) {
            if (this.isAgree()) {
                this.registerModel.active = true
            }
        }, this)

        // this.updateList()
    },

    // 获取更新内容
    updateList () {
        LoginManager.update((res) => {
            if (res.code === 200) {
                this.updateListMsg = res.msg
            }
        })
    },

    hotUpdateFn () {
        var self = this
        var normalStart = () => {
            // 预加载加载场景
            var pnode = cc.find('Canvas')

            cc.director.preloadScene('loading', () => {
                console.log('预加载场景Loading')
                let loginInfo = cc.sys.localStorage.getItem('__USERSTRING__')
                if (!UnitTools.isNullOrUndefined(loginInfo)) {
                    loginInfo = JSON.parse(loginInfo)

                    ClickManager.loading(pnode, '自动登录中...')
                    LoginManager.login(loginInfo, (res) => {
                        ClickManager.rmLoading(pnode)
                        if (res.code !== 200) {
                            TableManager.alertModel('自动登录失败')
                            cc.sys.localStorage.removeItem('__USERSTRING__')
                            cc.sys.localStorage.removeItem('__TOKEN__')
                        } else {
                            cc.sys.localStorage.setItem('__USERSTRING__', JSON.stringify(loginInfo))
                            cc.sys.localStorage.setItem('__TOKEN__', res['data']['token'])
                            cc.director.loadScene('loading')
                        }
                    })
                }
            })
        }

        if (!cc.sys.isNative) {
            normalStart()
            return;
        } else {
            var hot = new HotUpdate()
            hot.checkUpdate((isNew) => {
                if (isNew) {
                    var tipUI = cc.find('HotUpdate', self.node)
                    tipUI.active = true
                    var tipsText = cc.find('translucent/input_model/tipsText', tipUI)
                    tipsText.getComponent(cc.Label).string = '更新中: 0%'

                    hot.update(function () {//更新完成
                        cc.log('更新完成回调')
                        tipsText.getComponent(cc.Label).string = '更新完成'

                    },function () {//发生错误
                        cc.log('更新错误，请重试')
                        tipsText.getComponent(cc.Label).string = '更新错误，请重试'
                    },function (process) {
                        cc.log('更新中:'+process+'%')
                        tipsText.getComponent(cc.Label).string = '更新中: '+process+'%'
                    })
                }  else { // 没有新版本
                    normalStart()
                }
            })
        }
    },

    // 勾选协议
    isAgree () {
        if (!this.agreeBtn.isChecked) {
            TableManager.alertModel('请您先勾选协议')
            return false
        }

        return true
    }
});
